package Eyjafjalla.action;

import Eyjafjalla.helper.StringHelper;
import Eyjafjalla.modcore.EyjafjallaTag;
import Eyjafjalla.modifier.LovedMod;
import basemod.helpers.CardModifierManager;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.UIStrings;

import java.util.ArrayList;
import java.util.Iterator;

public class LovedAction extends AbstractGameAction {
    private static final UIStrings uiStrings;
    public static final String[] TEXT;
    private AbstractPlayer p;
    private static final float DURATION;
    boolean fired;

    public LovedAction(boolean fired) {
        this.p = AbstractDungeon.player;
        this.setValues(null, null, 1);
        this.actionType = ActionType.CARD_MANIPULATION;
        this.duration = DURATION;
        this.fired = fired;
    }

    ArrayList<AbstractCard> removed = new ArrayList<>();

    private void returnCards(){
        AbstractDungeon.player.hand.group.addAll(removed);
        removed.clear();
    }

    private void markLoved(AbstractCard c){
        CardModifierManager.addModifier(c,new LovedMod());
        if(fired){
            c.freeToPlayOnce = true;
        }
    }


    public void update() {
        if (this.duration == DURATION) {
            if (AbstractDungeon.getMonsters().areMonstersBasicallyDead()) {
                this.isDone = true;
                return;
            }
            for (AbstractCard c : AbstractDungeon.player.hand.group) {
                if (c.hasTag(EyjafjallaTag.LOVED_EYJAFJALLA)) {
                    removed.add(c);
                }
            }
            if (removed.size() == AbstractDungeon.player.hand.size()) {
                this.isDone = true;
                return;
            }

            for (AbstractCard c : removed) {
                AbstractDungeon.player.hand.group.remove(c);
            }
            AbstractDungeon.handCardSelectScreen.open(TEXT[8], this.amount, false, false);

            AbstractDungeon.player.hand.applyPowers();
            this.tickDuration();
            return;
        }

        if (!AbstractDungeon.handCardSelectScreen.wereCardsRetrieved) {
            Iterator<AbstractCard> var4 = AbstractDungeon.handCardSelectScreen.selectedCards.group.iterator();
            while (var4.hasNext()) {
                AbstractCard c = var4.next();
                markLoved(c);
                AbstractDungeon.player.hand.group.add(c);
            }
            returnCards();
            AbstractDungeon.player.hand.refreshHandLayout();
            AbstractDungeon.player.hand.applyPowers();

            AbstractDungeon.handCardSelectScreen.wereCardsRetrieved = true;
        }

        this.tickDuration();
    }

    static {
        uiStrings = CardCrawlGame.languagePack.getUIString(StringHelper.CLOUD_OPERATION);
        TEXT = uiStrings.TEXT;
        DURATION = Settings.ACTION_DUR_XFAST;
    }
}

